/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.mobileWeb.shopping;

import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.entity.shopping.EduShoppingCart;
import com.idenger.modules.edu.entity.shopping.ShoppingCart;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.shopping.EduShoppingCartService;
import com.idenger.modules.edu.entity.user.EduUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 * @author chenlin
 * @version 2017-12-14
 */
@Controller
@RequestMapping(value = "${mobilePath}/edu/shopping/eduShoppingCart")
public class MEduShoppingCartController extends BaseController {

	@Autowired
	private EduShoppingCartService eduShoppingCartService;
	@Autowired
	private EduCourseService eduCourseService;
	
	@ModelAttribute
	public EduShoppingCart get(@RequestParam(required=false) String id) {
		
		EduShoppingCart entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduShoppingCartService.get(id);
		}
		if (entity == null){
			entity = new EduShoppingCart();
		}
		return entity;
	}
	
	@RequestMapping(value = "selectlist")
	public String selectlist(EduShoppingCart eduShoppingCart,HttpServletRequest request, HttpServletResponse response){
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		List<ShoppingCart>  eduShoppingCartList=eduShoppingCartService.findShoppingCart(eduUser.getId());
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduShoppingCartList);
		return renderString(response, baseResponseMsg);
	}
	

	@RequiresPermissions("edu:shopping:eduShoppingCart:view")
	@RequestMapping(value = "form")
	public String form(EduShoppingCart eduShoppingCart, Model model) {
		model.addAttribute("eduShoppingCart", eduShoppingCart);
		return "modules/edu/shopping/eduShoppingCartForm";
	}
	
	
	//课程移除购物车
	@RequestMapping(value = "moveEduCourseShoppingCart") 
	public String moveEduCourseShoppingCart( HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		//拿到课程ID
		String courseId = request.getParameter("id");
		//根据课程ID 用户ID 查询对应的课程
		EduShoppingCart eduShoppingCart = new EduShoppingCart();
		eduShoppingCart.setUserId(eduUser.getId());
		eduShoppingCart.setCourseId(courseId);
		List<EduShoppingCart> eduShoppingCartList = eduShoppingCartService.forbidSameDataToCart(eduShoppingCart);
		for(EduShoppingCart eduSho :eduShoppingCartList ){
			eduShoppingCartService.delete(eduSho);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		return renderString(response, baseResponseMsg);
	}
	//项目移除购物车
	@RequestMapping(value = "moveEduProjectShoppingCart") 
	public String moveEduProjectShoppingCart(HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		//拿到课程ID
		String projectId = request.getParameter("id");
		//根据课程ID 用户ID 查询对应的课程
		EduShoppingCart eduShoppingCart = new EduShoppingCart();
		eduShoppingCart.setUserId(eduUser.getId());
		eduShoppingCart.setProjectId(projectId);
		List<EduShoppingCart> eduShoppingCartList = eduShoppingCartService.forbidSameDataToCart(eduShoppingCart);
		for(EduShoppingCart eduSho :eduShoppingCartList ){
			eduShoppingCartService.delete(eduSho);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		return renderString(response, baseResponseMsg);
	}
	
	
	//课程添加购物车
	@RequestMapping(value = "addToShoppingCart")
	public String addCourseToShoppingCart(EduShoppingCart eduShoppingCart , HttpServletRequest request, HttpServletResponse response){
		BaseResponseMsg baseResponseMsg;
		String courseId = request.getParameter("courseId");
		String projectId = request.getParameter("projectId");
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		List<ShoppingCart> shoppingCartList =	eduShoppingCartService.findShoppingCart(eduUser.getId());
		if(shoppingCartList.size()>30){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("数量过多，请先清空购物车");
			return renderString(response, baseResponseMsg);
		}
		eduShoppingCart.setUserId(eduUser.getId());
		if(StringUtils.isNotBlank(projectId)){
			//项目的添加
			eduShoppingCart.setProjectId(projectId);
//			eduShoppingCart.setCourseId("");
		}else{
			//课程的添加
			eduShoppingCart.setProjectId("");
			eduShoppingCart.setCourseId(courseId);
		}
		List<EduShoppingCart> eduShoppingCartList = eduShoppingCartService.forbidSameDataToCart(eduShoppingCart);
		if(StringUtils.isNotBlank(courseId)){
			if(eduShoppingCartList.size()>0){
				baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
				baseResponseMsg.addData("您已经添加过了");
				return renderString(response, baseResponseMsg);
			}
		}else{
			List<EduCourse> courses = eduCourseService.findProjectContentCourse(projectId);
			if(courses.size() == eduShoppingCartList.size()){
				baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
				baseResponseMsg.addData("您已经添加过了");
				return renderString(response, baseResponseMsg);	
			}
		}
		if(StringUtils.isNotBlank(projectId)){
			//项目的添加
			if(StringUtils.isNotBlank(courseId)){
				//添加选中的项目下的单个课程
				EduShoppingCart eduSh = new EduShoppingCart();
				eduSh.setProjectId(projectId);
				eduSh.setUserId(eduUser.getId());
				eduSh.setCourseId(courseId);
				eduShoppingCartService.save(eduSh);
			}else{
				//添加项目下的全部课程
				List<EduCourse> eduCourses = eduCourseService.findProjectContentCourse(projectId);
				for(EduCourse ecourse :eduCourses ){
					EduShoppingCart eduSh = new EduShoppingCart();
					eduSh.setProjectId(projectId);
					eduSh.setCourseId(ecourse.getId());
					eduSh.setUserId(eduUser.getId());
					List<EduShoppingCart> carts = eduShoppingCartService.forbidSameDataToCart(eduSh);
					if(carts.size() == 0){
						eduShoppingCartService.save(eduSh);
					}
				}
			}				
		}else{
			//课程的添加
			eduShoppingCartService.save(eduShoppingCart);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData("success");
		return renderString(response, baseResponseMsg);
	}
}